<?php

namespace app\award;

use app\models\NewLevel;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductArea;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class Award
{
    protected $order;
    protected $user;

    protected $set;

    // 重新计算佣金
    protected $is_reset;

    public function __construct(Order $order, $is_reset = false)
    {
        $this->is_reset = $is_reset;

        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);


        if ($is_bao) {
            $this->recommend();
        }



    }


    // 推荐奖
    public function recommend()
    {
        $name1 = '推荐奖';
        $type1 = 10;

        $name2 = '扶持奖励';
        $type2 = 20;

        $name3 = '出额';
        $type3 = 30;

        $name4 = '分红';
        $type4 = 40;

        $amount = $this->order->total_pay - $this->order->total_freight;

        $r = Chain::getReferrer($this->order->user_id, true);
        if ($r) {
            $commission = 0;

            // 是否为上级出
            $seller = false;

            // 购买VIP3
            if ($amount >= $this->set->vip3) {
                if ($r->level == UserLevel::$keys['vip1']) {
                    $commission = $this->set->vip1_3;
                }
                if ($r->level == UserLevel::$keys['vip2']) {
                    $commission = $this->set->vip2_3;
                }
                if ($r->level == UserLevel::$keys['vip3']) {
                    $commission = $this->set->vip3_3;
                }
            }

            // 购买VIP2
            else if ($amount >= $this->set->vip2) {
                if ($r->quota2 > 0) {
                    $seller = true;
                } else {
                    if ($r->level == UserLevel::$keys['vip1']) {
                        $commission = $this->set->vip1_2;
                    }
                    if ($r->level == UserLevel::$keys['vip2']) {
                        $commission = $this->set->vip2_2;
                    }
                    if ($r->level == UserLevel::$keys['vip3']) {
                        $commission = $this->set->vip3_2;
                    }
                }
            }

            // 购买VIP1
            else if ($amount >= $this->set->vip1) {
                if ($r->quota1 > 0) {
                    $seller = true;
                } else {
                    if ($r->level == UserLevel::$keys['vip1']) {
                        $commission = $this->set->vip1_1;
                    }
                    if ($r->level == UserLevel::$keys['vip2']) {
                        $commission = $this->set->vip2_1;
                    }
                    if ($r->level == UserLevel::$keys['vip3']) {
                        $commission = $this->set->vip3_1;
                    }
                }
            }

            if ($seller && $r->level >= UserLevel::$keys['vip2']) {
                // 出货
                $this->saveCommission($r->id, $amount, $type3, $name3);
                if (!$this->is_reset) {
                    if ($amount >= $this->set->vip2) {
                        $r->quota2 -= 1;
                    } elseif ($amount >= $this->set->vip1) {
                        $r->quota1 -= 1;
                    }
                    $r->save();
                }
            } else {
                $this->saveCommission($r->id, $commission, $type1, $name1);
                // 扶持奖励
                if ($r->level == UserLevel::$keys['vip3']) {
                    $this->saveCommission($r->id, $amount * $this->set->help_ratio / 100, $type2, $name2);
                } else {
                    // $r2 = Chain::getReferrer($r->id, true);
                    // if ($r2 && $r2->level == UserLevel::$keys['vip3']) {
                    //     $this->saveCommission($r2->id, $commission * $this->set->help_ratio / 100, $type2, $name2);
                    // }
                }
                // 分红
                $list = User::find()
                    ->where(['level' => UserLevel::$keys['vip3']])
                    ->orderBy(['id' => SORT_ASC])
                    ->limit($this->set->profit_num)
                    ->all();
                if ($list) {
                    $commission = $amount * $this->set->profit_ratio / 100 / count($list);
                    foreach ($list as $user) {
                        $this->saveCommission($user->id, $commission, $type4, $name4);
                    }
                }
            }
        }

    }


    // 保存佣金
    protected function saveCommission($user_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->order->user_id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = 1;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                $account->balance += $balance;
                $account->save();
            }
        }
    }
}